package com.fsx.servertokenlogin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fsx.servertokenlogin.config.JwtConfg;
import com.fsx.servertokenlogin.dao.LoginDao;
import com.fsx.servertokenlogin.entity.User;
import com.fsx.servertokenlogin.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class LogingServiceImpl implements ILoginService {

    @Autowired
    LoginDao loginDao;

    @Autowired
    JwtConfg jwtConfg;

    @Autowired
    LoginDao dao;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public String login(String uid, String pwd,String fingerprint) {
        String token=jwtConfg.createToken(uid,fingerprint);
        if(dao.selectList(new QueryWrapper<User>().eq("t_id",uid)).size()==0){
            throw new RuntimeException("账号不纯在");
        }else {
            if(dao.selectList(new QueryWrapper<User>().eq("t_id",uid).eq("t_pwd",pwd)).size()==0){
                throw new RuntimeException("密码有误");
            }
        }
        redisTemplate.opsForValue().set(uid,token,7, TimeUnit.DAYS);
        return token;
    }
}
